package com.tjhq.commons.dict.util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.*;

/**
 * Created by pc on 2017/11/21.
 */
public class DictUtils {
    
    /**
     * jsonList to List<Map>
     * @param jsonList
     * @throws IOException
     * @throws Exception
     */
    public static List JsonToMap(String jsonList) throws IOException, Exception {
        List l = new ArrayList();
        JSONArray jsonArray = new JSONArray(jsonList);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Map map = new HashMap();
            Iterator keyIter = jsonObject.keys();
            while (keyIter.hasNext()) {
                String key = (String) keyIter.next();
                Object value = (Object) jsonObject.get(key);
                if (null != value && JSONObject.NULL != value
                        && !"undefined".equals(value)) {
                    map.put(key, DictDBConstants.fromEncodedUnicode(value
                            .toString().toCharArray(), 0, value.toString()
                            .length()));
                } else {
                    map.put(key, null);
                }
            }
            l.add(map);
        }
        return l;
    }
}
